package com.zsrt.javaweb;

import java.io.IOException;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * 字符编码过滤器
 * @author scott
 *
 */
public class EncodingFilter extends HttpFilter {

	
	private String encoding;
	
	@Override
	public void initChildren() {
		System.out.println("进入EncodingFilter子类的init方法...");
		
		this.encoding = getFilterConfig().getServletContext().getInitParameter("encoding");
		
		System.out.println("取得上下文参数encoding="+encoding);
		System.out.println("取得过滤器参数encoding="+getFilterConfig().getInitParameter("encoding"));
		
	}
	
	
	@Override
	public void doHttpFilter(HttpServletRequest request, HttpServletResponse response, FilterChain chain)
			throws IOException, ServletException {
		System.out.println("进入字符编码过滤器...");
		
		request.setCharacterEncoding(this.encoding);
		
		chain.doFilter(request, response);

	}

}
